{include file="public/header" /}

<body style="background-color: #ffffff">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>广告名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="title" name="title" required="" lay-verify="required" autocomplete="off"
                            class="layui-input" value="{$row.title|default=''}" placeholder="请输入选择名称">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>商家排序[数值越大靠前]</label>
                    <div class="layui-input-inline">
                        <input type="number" id="sorts" name="sorts" required="" lay-verify="required"
                            autocomplete="off" class="layui-input" value="{$row.sorts|default='0'}"
                            placeholder="请输入商家排序">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        是否显示</label>
                    <div class="layui-input-inline">
                        <select name="ishow" lay-verify="required" lay-filter="type">
                            <option value="1" {if !empty($row.ishow) && $row.ishow==1}selected{/if}>显示</option>
                            <option value="2" {if !empty($row.ishow) && $row.ishow==2}selected{/if}>隐藏</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        轮播类型</label>
                    <div class="layui-input-inline">
                        <select name="types" lay-verify="required">
                            <option value="1" {if !empty($row.types) && $row.types==1}selected{/if}>顶部轮播图</option>
                            <option value="2" {if !empty($row.types) && $row.types==2}selected{/if}>中间轮播图</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        类型</label>
                    <div class="layui-input-inline">
                        <select name="type" lay-verify="required" lay-filter="type">
                            <option value="0">不跳转</option>
                            <option value="1" {if !empty($row.type) && $row.type==1}selected{/if}>跳转商品</option>
                            <option value="2" {if !empty($row.type) && $row.type==2}selected{/if}>跳转分类</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="getid" {if empty($row.type)}style="display: none;" {/if}>
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>选择分类|商品</label>
                    <div class="layui-input-inline">
                        <select name="getid" lay-verify="required" lay-filter="getid">
                            <option value="0">选择</option>
                            {if !empty($row.list)}
                            {foreach $row.list as $v}
                            <option value="{$v.id}" {if !empty($row.getid) && $row.getid==$v.id}selected{/if}>
                                {$v.title}
                            </option>
                            {/foreach}
                            {/if}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="cover" class="layui-form-label">
                        <span class="x-red">*</span>图片</label>
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn  layui-btn-normal" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                            <input type="hidden" value="{$row.banner|default=''}" name="banner" id="covers">
                            <img class="layui-upload-img" id="demo1" src="{$row.banner|default=''}"
                                style="width: 200px;height: auto;">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <input type="hidden" value="{$row.id|default=''}" name="id">
                    <button class="layui-btn  layui-btn-normal" lay-filter="add" lay-submit="">提交</button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <button type="button" class="layui-btn layui-btn-danger" onclick="IframeClose()">取消</button>
                </div>
            </form>
        </div>
    </div>

    {include file="public/footer" /}
    <script src="/static/leyedit/Layui-KnifeZ/layui.js"></script>
    <script src="/static/leyedit/ace/ace.js"></script>
    <script>
        layui.use(['layedit', 'layer', 'jquery', 'form', 'upload'], function () {
            $ = layui.jquery;
            var form = layui.form
                , upload = layui.upload
                , layer = layui.layer;
            var layedit = layui.layedit;
            //图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/Base/upload_img'
                , accept: 'images'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        // $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $('#covers').val(res.data.src);
                    $('#demo1').attr('src', res.data.src);
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            form.on('select(shopid)', function (data) {
                var type = $('select[name="type"]').val();
                $.ajax({
                    type: 'get',
                    url: '/base/gettypes',
                    data: { type: type, shopid: data.value },
                    success: function (res) {
                        if (res.code == 0) {
                            str = '';
                            str += '<option value="">请选择</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.id + '">' + obj.title + '</option>';
                            });
                            $('select[name="getid"]').html(str);
                            form.render();
                        } else {
                            str = '';
                            form.render();
                        }
                    }
                });
                return false;
            });
            form.on('select(type)', function (data) {
                var shopid = $('select[name="shopid"]').val();
                if (!shopid) {
                    layer.msg('请先选择商家', { icon: 2, time: 1000 });
                    return false;
                }
                if (!data.value || data.value == 0) {
                    $('#getid').hide();
                    return false;
                }
                $('#getid').show();
                $.ajax({
                    type: 'get',
                    url: '/base/gettypes',
                    data: { type: data.value, shopid: shopid },
                    success: function (res) {
                        if (res.code == 0) {
                            str = '';
                            str += '<option value="">请选择</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.id + '">' + obj.title + '</option>';
                            });
                            $('select[name="getid"]').html(str);
                            form.render();
                        } else {
                            str = '';
                            form.render();
                        }
                    }
                });
                return false;
            });
            //监听提交
            form.on('submit(add)', function (data) {
                $.ajax({
                    'type': 'post',
                    data: data.field,
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, { icon: 6, time: 1000 }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
                return false;
            });
        });
    </script>

</body>

</html>